package com.zuxia.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.hibernate.criterion.DetachedCriteria;
import org.springframework.stereotype.Service;

import com.zuxia.dao.IStockDao;
import com.zuxia.entity.Acmodel;
import com.zuxia.entity.Car;
import com.zuxia.entity.Stock;
import com.zuxia.service.IStockService;
import com.zuxia.uilt.Page;

/**
 * ���ҵ���ʵ����
 */
@Service("stockService")
public class StockServiceImpl implements IStockService {
	@Resource(name = "stockDao")
	private IStockDao stockDao;

	/**
	 * ��ӿ��ҵ��
	 */
	public String saveStock(Stock s) {
		if (stockDao.saveStock(s) > 0) {
			return "saveStock";
		}
		return "error";
	}

	/**
	 * ��ѯ���ͺ�����ͺ�
	 */
	public Map<String, Object> selectCarModel() {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Car> carss = stockDao.findCarModel();
		List<Acmodel> acmodelss = stockDao.findModel();
		map.put("carss", carss);
		map.put("acmodelss", acmodelss);
		return map;
	}

	/**
	 * ��ѯȫ��
	 */
	public List<Stock> findAll() {
		return stockDao.findAll();
	}

	/**
	 * ��ѯ��������
	 */
	public Stock findStockById(Stock stock) {
		return stockDao.findStockById(stock);
	}

	/**
	 * �޸�stock
	 */
	public String updateStock(Stock stock) {
		int i = stockDao.updateStock(stock);
		if (i > 0) {
			return "updateStock";
		}
		return "error";
	}

	/**
	 * ɾ��
	 */
	public boolean deleteStock(Stock stock) {
		int i = stockDao.deleteStock(stock);
		if (i > 0) {
			return true;
		}
		return false;
	}

	/**
	 * ����ɾ��
	 */
	public boolean batchDelteStock(String checks) {
		try {
			stockDao.batchDelteStock(checks);
		} catch (Exception e) {
			return false;
		}
		return true;
	}

	public Page findPageStock(DetachedCriteria dCriteria, Integer num) {
		// 1.准备当前页信息
		int currentPageNum = 1;
		if (num != null) {
			currentPageNum = num;
		}
		// 2.获取总记录条数
		int totalRecords = stockDao.CountStock();
		// 3.创建一个page对象
		Page page = new Page(currentPageNum, totalRecords);
		// 4.使用page对象中的数据，查询带有分页的结果集
		List<Stock> records = stockDao.findPageStokc(page.getStartIndex(), page.getPageSize(), dCriteria);
		// 5.把查询出来的结果集封装到page对象中
		page.setRecords(records);
		// 6.返回page对象
		return page;
	}

}
